
#include "Header.h"
#include <math.h>

struct DistortionDataStruct
{
	int Channels;
	int Length;
	double Output;
	double Dist;
} typedef DistortionData;


DECLSPEC void STDCALL Effect_Distortion_Process(DistortionData* data,
	double* wav_in_left, double* wav_in_right, double* wav_out_left, double* wav_out_right)
{
	double a = data->Dist > 0.98 ? 0.98 : data->Dist;
	double k = 2 * a / (1 - a);
	double output = data->Output;
	double k1 = k + 1;

	// Reference: http://www.musicdsp.org/archive.php?classid=4
	for (int c = data->Channels - 1; c >= 0; c--)
	{
		double* inputSamples = c == 0 ? wav_in_left : wav_in_right;
		double* outputSamples = c == 0 ? wav_out_left : wav_out_right;

		for (int i = data->Length - 1; i >= 0; i--)
		{
			double x = inputSamples[i];
			outputSamples[i] = (k1 * x / (1 + k * fabs(x))) * output;
		}
	}
}